
/**
 * Created by dukai on 2017/5/11.
 */

var mSettingParam = {
};

(function(doc, win) {

    // sui 页面就绪时间
    $(doc).on("pageInit", "#mineSettingPage", function() {
        var controller = new MineSettingController();
        controller.getAppVersion();
    });

    $(doc).on("pageInit", "#mineModifyPhonePage", function() {
        $("#mineModifyPhonePage #password").val("");
        $("#mineModifyPhonePage #phoneNum").val("");
        $("#mineModifyPhonePage #verifyCode").val("");

    });

    $(doc).on("pageInit", "#mineModifyPwdPage", function() {
        $("#mineModifyPwdPage #oldPassword").val("");
        $("#mineModifyPwdPage #newPassword").val("");
        $("#mineModifyPwdPage #newPasswordConfirm").val("");
    });


    $(doc).on("click", "#mineSettingPage ul li", function() {

        log4JS.error("点击列表项");
       /* var pageUrl = $(this).attr("href");

        alert(pageUrl);
        if(pageUrl && pageUrl != "javascript:void(0)"){
            $.router.load(pageUrl);
        }else if(pageUrl == "javascript:void(0)"){
            window.location.href = "tel://400123412312" ;
        }*/
    });


    //点击获取验证码按钮
    $(doc).on('click','#mineModifyPhonePage .get-code',function(){

        var phoneNum = $("#mineModifyPhonePage #phoneNum").val();

        //手机号校验
        if(!check4SendVerifyCode(phoneNum)){return;}

        //倒计时60秒
        if($(this).hasClass('disabled')){
            return;
        }
        /*
        commomWaitSecond({
            second:60,
            target:$(".get-code"),
            normalText:"获取验证码",
            waiting:function(second){
                return second + "秒后重发";
            }
        });*/

        var controller = new MineSettingController();
        controller.sendVerifyCode(phoneNum);
    });


    //控制按钮 可用 与 不可用（置灰） start
    $(doc).on('keyup','#mineModifyPhonePage #password,' +
        '#mineModifyPhonePage #phoneNum,' +
        '#mineModifyPhonePage #verifyCode',function(){

        // oninput="if(value.length>6)value=value.slice(0,6)"

        if($(this).attr("id")=="phoneNum" && $(this).val().length>11){
            $(this).val($(this).val().slice(0,11));
        }

        if($(this).attr("id")=="verifyCode" && $(this).val().length>6){
            $(this).val($(this).val().slice(0,6));
        }

        var password = $("#mineModifyPhonePage #password").val();
        var phoneNum = $("#mineModifyPhonePage #phoneNum").val();
        var verifyCode = $("#mineModifyPhonePage #verifyCode").val();

        if(password && phoneNum && verifyCode){
            $("#mineModifyPhonePage #modifyPhoneBtn").removeClass("headBtnDisabled");
        }else{
            $("#mineModifyPhonePage #modifyPhoneBtn").addClass("headBtnDisabled");
        }
    });


    $(doc).on('keyup','#mineModifyPwdPage #oldPassword,' +
        '#mineModifyPwdPage #newPassword,' +
        '#mineModifyPwdPage #newPasswordConfirm',function(){

        var oldPassword = $("#mineModifyPwdPage #oldPassword").val();
        var newPassword = $("#mineModifyPwdPage #newPassword").val();
        var newPasswordConfirm = $("#mineModifyPwdPage #newPasswordConfirm").val();

        if(oldPassword && newPassword && newPasswordConfirm){
            $("#mineModifyPwdPage #modifyPwdBtn").removeClass("headBtnDisabled");
        }else{
            $("#mineModifyPwdPage #modifyPwdBtn").addClass("headBtnDisabled");
        }
    });

    $(doc).on('click','#mineModifyPwdPage .zhk-list_gb',function(){
        $("#mineModifyPwdPage #modifyPwdBtn").addClass("headBtnDisabled");
    });

    $(doc).on('click','#mineModifyPhonePage .zhk-list_gb',function(){
        $("#mineModifyPhonePage #modifyPhoneBtn").addClass("headBtnDisabled");
    });

    //控制按钮 可用 与 不可用（置灰） end


    $(doc).on('click','#mineModifyPhonePage #modifyPhoneBtn',function(){

        //前端校验手机号
        var password = $("#mineModifyPhonePage #password").val();
        var phoneNum = $("#mineModifyPhonePage #phoneNum").val();
        var verifyCode = $("#mineModifyPhonePage #verifyCode").val();

        //判断按钮是否可用
        if(!(password && phoneNum && verifyCode)){
            return;
        }

        //前端校验
        if(!check4ModifyPhone(password, phoneNum, verifyCode)){return;}

        var controller = new MineSettingController();
        controller.modifyPhone(password,phoneNum,verifyCode);
    });


    $(doc).on('click','#mineModifyPwdPage #modifyPwdBtn',function(){

        var oldPassword = $("#mineModifyPwdPage #oldPassword").val();
        var newPassword = $("#mineModifyPwdPage #newPassword").val();
        var newPasswordConfirm = $("#mineModifyPwdPage #newPasswordConfirm").val();

        //判断按钮是否可用
        if(!(oldPassword && newPassword && newPasswordConfirm)){
            return;
        }

        //前端校验
        if(!check4ModifyPwd(oldPassword, newPassword, newPasswordConfirm)){return;}

        var controller = new MineSettingController();
        controller.modifyPwd(oldPassword, newPassword);
    });


    //返回按钮
    /*$(doc).on('click','#mineModifyPwdPage .left',function(){
        $.router.load("./mine-setting.html");
    });

    $(doc).on('click','#mineModifyPhonePage .left',function(){
        $.router.load("./mine-setting.html");
    });

    $(doc).on('click','#aboutUSPage .left',function(){
        $.router.load("./mine-setting.html");
    });*/


    //列表事件
    //用户注销
    $(doc).on('click','#mineSettingPage .warn',function(){

        var param = {
            title : "",
            content : "您确定要退出登录吗？",
            btnLeft : "确定",
            btnRight : "取消",
            callbackLeft:function () {
                log4JS.info("点击退出按钮，调用native函数执行退出操作");

                AppUtil.call(AppUtil.APIS.LOGOUT,{},function(result){

                    log4JS.info("回调返回值" + result);

                    if(result==-1||result=='-1'){
                        DialogUtil.dialogWarn("退出登录失败！");
                    }
                });
            },
            callbackRight:function () {
                return;
            }
        };

        DialogUtil.dialogConfirm(param);
    });

    //联系客服
    $(doc).on('click','#contactUs',function(){
        // var controller = new MineSettingController();
        // controller.dialNumber();
        // AppUtil.call(AppUtil.APIS.DIAL,{"mobile":"4001099960"});

        common.loading();
        AppUtil.call(AppUtil.APIS.DIAL,{"mobile":"4001099960"},function(result){

            log4JS.info("拨号回调返回值" + result);

            if(result==-1||result=='-1'){
                common.closeLoading();
                DialogUtil.dialogWarn("拨号失败！");
            }else{
                common.closeLoading();
            }
        });

        /*setTimeout(function () {
            common.closeLoading();
        }, 1000);*/

    });

    //关于我们
    $(doc).on('click','#aboutUs',function(){
        AppUtil.call(AppUtil.APIS.PUSH_WINDOW,
            {
                url: 'vuepages/index.html#/about',
                target: 'local',
                param: {
                    title: null
                }
            });
    });

    //修改密码
    $(doc).on('click','#modifyPwd',function(){
        $.router.load("./mine-modify-password.html");
    });

    //修改手机号
    $(doc).on('click','#modifyMobile',function(){
        $.router.load("./mine-modify-phone.html");
    });


    //手机号格式检查
    var check4SendVerifyCode = function (phoneNum){

        // var regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var regex = /^1\d{10}$/;
        if(!phoneNum){
            DialogUtil.dialogWarn("请输入手机号");
            return false;
        }else if(!regex.test(phoneNum)){
            DialogUtil.dialogWarn("请输入正确的手机号");
            return false;
        }
        return true;
    };


    var check4ModifyPhone = function (password, phoneNum, verifyCode){
        if(password == ""){
            DialogUtil.dialogWarn("请输入密码",null,null);
            return false;
        }

        var reg =  /^[a-zA-z0-9~!@#$%^&*()_+-={}|\[\]:";'<>?,.]{6,14}$/;

        if(!reg.test(password)){
            // DialogUtil.dialogWarn("密码格式不正确，应为6-14位英文、数字组合",null,null);
            DialogUtil.dialogWarn("密码格式不正确，应为6~14位数字、字母、标点符号",null,null);
            return false;
        }

        if(!verifyCode){
            DialogUtil.dialogWarn("请填写验证码",null,null);
            return false;
        }

        // var regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var regex = /^1\d{10}$/;
        if(!phoneNum){
            DialogUtil.dialogWarn("请输入手机号");
            return false;
        }else if(!regex.test(phoneNum)){
            DialogUtil.dialogWarn("请输入正确的手机号");
            return false;
        }
        return true;
    };


    //密码格式检查
    var check4ModifyPwd = function (oldPassword, newPassword, newPasswordConfirm){

        // if(oldPassword.trim() == "" || newPassword.trim() == "" || newPasswordConfirm.trim() == ""){
        if(oldPassword == "" || newPassword == "" || newPasswordConfirm == ""){
            DialogUtil.dialogWarn("请输入密码",null,null);
            return false;
        }

        if(newPasswordConfirm != newPassword){
            DialogUtil.dialogWarn("两次输入密码不一致，请重新输入",null,null);
            return false;
        }

        //密码正则
        var reg =  /^[a-zA-z0-9~!@#$%^&*()_+-={}|\[\]:";'<>?,.]{6,14}$/;

        if(!reg.test(oldPassword) || !reg.test(newPassword)){
            // DialogUtil.dialogWarn("密码格式不正确，应为6-14位英文、数字组合",null,null);
            DialogUtil.dialogWarn("密码格式不正确，应为6~14位数字、字母、标点符号",null,null);
            return false;
        }
        return true;
    };

    var getPayUrlParam = function (key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);

        if(r && r.length > 3){
            return decodeURIComponent(r[2]);
        }else{
            log4JS.error("参数传入错误");
            DialogUtil.dialogWarn("参数传入错误");
            return null;
        }
    };

})(document, window);
